package net.cyue.nest.nestjava.core.injector;

import net.cyue.nest.nestjava.common.data.MetaType;
import net.cyue.nest.nestjava.common.util.ReflectUtil;

public class InstanceWrapper
{
  private static long INSTANCE_COUNT = 0;

  public final long id;
  public final String name;
  public final ModuleData host;
  public MetaType metaType;
  private Object instance;

  public InstanceWrapper(
    MetaType metaType,
    String name,
    ModuleData host,
    Object instance
  ) {
    this.metaType = metaType;
    this.id = ++InstanceWrapper.INSTANCE_COUNT;
    this.name = name;
    this.host = host;
    this.instance = instance;
    if (this.instance == null) {
      this.instance = this._new();
    }
  }


  public void setInstance(Object instance) {
    this.instance = instance;
  }
  public Object getInstance() {
    return this.instance;
  }
  public Object _new() {
    Class<?> clz = this.metaType.getClz();
    return ReflectUtil.createInstance(clz);
  }

}

